<template>
  <div class="collection-card" v-if="collection != null">
    <!-- 图片容器 -->
    <div class="image-container">
      <img 
        :src="collection.cover" 
        :alt="collection.title"
        class="activity-cover"
        @error="handleImageError"
      />
      <!-- 分类标签 (如果有分类信息的话) -->
      <span class="category-tag" v-if="collection.category">
        {{ collection.category }}
      </span>
    </div>
    
    <!-- 内容区 -->
    <div class="content-area">
      <h3 class="activity-title" :title="collection.title">
        {{ truncatedTitle }}
      </h3>
      
      <!-- 可选：添加活动地点或时间 -->
      <div class="activity-meta" v-if="collection.location || collection.date">
        <span class="location" v-if="collection.location">
          <i class="location-icon"></i>{{ collection.location }}
        </span>
        <span class="date" v-if="collection.date">
          <i class="date-icon"></i>{{ collection.date }}
        </span>
      </div>
    </div>
  </div>
</template>

<script setup>
import { defineProps, computed, ref } from "vue";

const props = defineProps({
  collection: {
    type: Object,
    required: true,
  },
  maxTitleLength: {
    type: Number,
    default: 16
  }
});

// 处理标题截断，带有计算属性
const truncatedTitle = computed(() => {
  if (!props.collection.title) return '';
  return props.collection.title.length > props.maxTitleLength
    ? `${props.collection.title.substring(0, props.maxTitleLength)}...`
    : props.collection.title;
});

// 处理图片加载失败
const handleImageError = (e) => {
  e.target.src = '/path/to/placeholder-image.jpg'; // 设置默认占位图
};
</script>

<style scoped lang="less">
.collection-card {
  width: 100%;
  border-radius: 12px;
  overflow: hidden;
  background-color: #fff;
  transition: transform 0.3s ease, box-shadow 0.3s ease;
  height: 100%;
  display: flex;
  flex-direction: column;
  
  &:hover {
    transform: translateY(-4px);
  }
}

.image-container {
  position: relative;
  width: 100%;
  padding-top: 66%; // 3:2 宽高比
  overflow: hidden;
  
  .activity-cover {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    object-fit: cover;
    transition: transform 0.5s ease;
  }
  
  &:hover .activity-cover {
    transform: scale(1.05);
  }
  
  .category-tag {
    position: absolute;
    top: 12px;
    right: 12px;
    background: rgba(0, 0, 0, 0.6);
    color: #fff;
    padding: 4px 8px;
    font-size: 12px;
    border-radius: 4px;
    font-weight: 500;
  }
}

.content-area {
  padding: 12px 16px;
  flex-grow: 1;
  display: flex;
  flex-direction: column;
}

.activity-title {
  font-size: 16px;
  font-weight: 600;
  color: #333;
  margin: 0 0 8px;
  line-height: 1.4;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  
  // 多行文本截断备选方案
  /* 
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  white-space: normal;
  height: 44px;
  */
}

.activity-meta {
  display: flex;
  align-items: center;
  font-size: 12px;
  color: #666;
  margin-top: auto;
  
  .location, .date {
    display: flex;
    align-items: center;
    margin-right: 12px;
  }
  
  .location-icon, .date-icon {
    display: inline-block;
    width: 14px;
    height: 14px;
    margin-right: 4px;
    background-position: center;
    background-repeat: no-repeat;
    background-size: contain;
  }
  
  .location-icon {
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' width='14' height='14' fill='%23666'%3E%3Cpath d='M12 2C8.13 2 5 5.13 5 9c0 5.25 7 13 7 13s7-7.75 7-13c0-3.87-3.13-7-7-7zm0 9.5c-1.38 0-2.5-1.12-2.5-2.5S10.62 6.5 12 6.5s2.5 1.12 2.5 2.5-1.12 2.5-2.5 2.5z'/%3E%3C/svg%3E");
  }
  
  .date-icon {
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' width='14' height='14' fill='%23666'%3E%3Cpath d='M19 3h-1V1h-2v2H8V1H6v2H5c-1.11 0-1.99.9-1.99 2L3 19c0 1.1.89 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zm0 16H5V8h14v11zM7 10h5v5H7z'/%3E%3C/svg%3E");
  }
}

// 响应式调整
@media (max-width: 768px) {
  .image-container {
    padding-top: 56%; // 16:9 宽高比，在小屏幕上占更少的垂直空间
  }
  
  .content-area {
    padding: 10px 12px;
  }
  
  .activity-title {
    font-size: 14px;
  }
}
</style>